package jf.jextn.io;

import java.util.Properties;

import javax.naming.InitialContext;
import javax.naming.NamingException;

import jf.jextn.JExtn;

public class JndiUtil {

    public static InitialContext create(Properties configuration, String propertyNamePrefix)
            throws NamingException {
        if (JExtn.hasValue(propertyNamePrefix)) {
            configuration = JExtn.subsetOf(configuration, propertyNamePrefix, true);
        }

        return create(configuration);
    }

    public static InitialContext create(String uri) {
        Properties config = ResourceUtil.getConfigurationByUrl(uri);
        InitialContext ctx = null;

        try {
            ctx = create(config, "jndi.");
        } catch (NamingException e) {
            throw new IllegalArgumentException("Not able to create initial context"
                    + " with properties defined by '" + uri + "'");
        }

        return ctx;
    }

    public static InitialContext create(Properties properties) throws NamingException {
        return new InitialContext(properties);
    }
}
